﻿using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; // <- this enables the use of DllImport
using System.IO;
using MKIB3Game;

using System.Windows;

namespace GuiConny
{
    class Pathfinder
    { 
        [DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern void noParameter();

        [DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int giveMeFive();

        [DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int oneSimpleParameter(int i);

        [DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int lastElement(int[] arr, int length);

        [DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr findPath(int from, int to, int[] map, int maph, int mapw, int plength);

        [DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern void freeArray(IntPtr pointer);


        public static MovementEnum[] getPath(Point destination, Point current, Map map)
        {
            int mapw = map.getWidth();
            int maph = map.getHeigth();
            int plength = maph * mapw;
            int boolmappointer = 0;
            int from = (int)((current.X * maph) + current.Y);
            int to = (int)((destination.X * maph) + destination.Y); 

            int[] boolmap = new int[plength];
            int temp=0;

            for( int w = 0; w<mapw;w++){
                for ( int h = 0; h< maph; h++){
                if(map.getMapCell(w, h).getCellPropertyArrayList().Contains(MapCellPropertyEnum.WALKABLE))// Abfrage hinzu, änderung in int[]
                temp=1;
                else
                temp =0;
                boolmap[boolmappointer] = temp;
                boolmappointer++;                
                }
            }
            Console.WriteLine("check succsess: " + plength + " =? " + boolmappointer);
            //Console.WriteLine("calling noParameter()");
            //noParameter();
            //Console.WriteLine("Calling give me five");
            //giveMeFive();




            Console.WriteLine("hey dll, gith me a path");
            IntPtr intpointer =  findPath(from, to, boolmap, maph, mapw, plength);
            
            // iwas mit intPtr machen
            int[] path = new int[100];
            Marshal.Copy(intpointer, path, 0, path.Length);
            freeArray(intpointer);
            //foreach (int i in path) Console.Out.Write(i);
            Array.Reverse(path);
            List<MovementEnum> pathToWalk = new List<MovementEnum>();
            foreach(int i in path){
                switch (i)
                {
                    case 0: pathToWalk.Add(MovementEnum.RIGHT); break;
                    case 1: pathToWalk.Add(MovementEnum.DOWN); break;
                    case 2: pathToWalk.Add(MovementEnum.LEFT); break;
                    case 3: pathToWalk.Add(MovementEnum.UP); break;
                    default: break;

                }
            } 
            return pathToWalk.ToArray();



        } 

    }
}


